package per.hx.bs.web.yw.controller;

import com.alibaba.fastjson.JSON;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;

import per.hx.bs.constant.DictTpye;

import per.hx.bs.entity.sys.SysDictDO;

import per.hx.bs.entity.sys.SysFileDO;
import per.hx.bs.entity.yw.YwAppraisalApplyDO;
import per.hx.bs.entity.yw.YwAppraisalDataDO;

import per.hx.bs.entity.yw.YwAppraisalItemDO;
import per.hx.bs.util.PageUtils;
import per.hx.bs.util.Query;

import per.hx.bs.web.sys.service.SysDictService;

import per.hx.bs.web.sys.service.SysFileService;
import per.hx.bs.web.yw.service.YwAppraisalApplyService;
import per.hx.bs.web.yw.service.YwAppraisalService;

import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/4/19  13:19
 */
@Controller
@RequestMapping(value = "yw/appraisalApply")
public class AppraisalApplyController extends BaseController {

    private final String prefix = "yw/appraisalApply";

    private final
    YwAppraisalApplyService applyService;

    private final
    SysDictService dictService;

    private final
    YwAppraisalService appraisalService;

    private final
    SysFileService fileService;

    @Autowired
    public AppraisalApplyController(YwAppraisalApplyService applyService, SysDictService dictService, YwAppraisalService appraisalService, SysFileService fileService) {
        this.applyService = applyService;
        this.dictService = dictService;
        this.appraisalService = appraisalService;
        this.fileService = fileService;
    }

    @BsCLog("进入考核指标申请审核界面")
    @RequiresPermissions("yw:apply:apply")
    @GetMapping(value = {"/apply", "/"})
    public String index() {
        return prefix + "/apply";
    }

    @BsCLog("获取考核指标申请审核数据")
    @RequiresPermissions("yw:apply:apply")
    @GetMapping(value = "/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<YwAppraisalApplyDO> applyDOS = applyService.list(query);
        int total = applyService.count(params);
        System.out.println(JSON.toJSONString(new PageUtils(applyDOS, total)));
        return new PageUtils(applyDOS, total);
    }

    @BsCLog("进入考核指标申请审核界面")
    @RequiresPermissions("yw:apply:add")
    @GetMapping(value = "/add")
    public String applyAdd() {
        return prefix + "/applyAdd";
    }

    @BsCLog("进入考核指标申请审核界面")
    @RequiresPermissions("yw:apply:add")
    @PostMapping(value = "/apply")
    @ResponseBody
    public ReturnDTO applyAdd(YwAppraisalApplyDO applyDO) {
        applyDO.setCreateId(getUserId());
        if (applyDO.getAmount() != null) {
            applyDO.setAmount(1);
        }
        return ReturnDTO.check(applyService.insert(applyDO));
    }

    @BsCLog("进入考核指标申请审核界面")
    @RequiresPermissions("yw:apply:edit")
    @GetMapping(value = "/applyEdit/{applyId}")
    public String applyEdit(@PathVariable("applyId") Long applyId, Model model) {
        YwAppraisalApplyDO applyDO = applyService.selectOne(applyId);
        if (applyDO == null) {
            return "redirect:apply";
        }
        List<SysDictDO> dictDOS = dictService.listByType(DictTpye.APPRAISAL_APPLY_STATE);
        appraisalItem(model, applyDO);
        List<SysFileDO> fileDOS = null;
        YwAppraisalDataDO dataDO = null;
        Long dataId = applyDO.getDataId();
        if (applyDO.getDataId() != null) {
            dataDO = applyService.selectOneData(dataId);
            String fileIds = dataDO.getFileId();
            if (fileIds != null) {
                String[] strings = fileIds.split(",");
                if (strings.length > 0) {
                    Long[] ids = (Long[]) ConvertUtils.convert(strings, Long.class);
                    fileDOS = fileService.selectList(ids);
                }
            }
        }
        model.addAttribute("apply", applyDO);
        model.addAttribute("dataDO", dataDO);
        model.addAttribute("fileDOS", fileDOS);
        model.addAttribute("dictS", dictDOS);
        return prefix + "/applyEdit";
    }

    @BsCLog("考核指标申请审核")
    @RequiresPermissions("yw:apply:edit")
    @PostMapping(value = "/applyEdit")
    @ResponseBody
    public ReturnDTO applyEdit(Long id, int amount, String state, String stateName,
                               String checkMessage) {
        YwAppraisalApplyDO applyDO = new YwAppraisalApplyDO();
        applyDO.setId(id);
        applyDO.setAmount(amount);
        applyDO.setState(state);
        applyDO.setStateName(stateName);
        applyDO.setCheckMessage(checkMessage);
        applyDO.setCheckId(getUserId());
        applyDO.setCheckName(getName());
        applyDO.setUpdateId(getUserId());
        return ReturnDTO.check(applyService.update(applyDO));
    }

    @BsCLog("查看申请详情界面")
    @RequiresPermissions("yw:apply:apply")
    @GetMapping(value = "/applyDetail/{applyId}")
    public String applyDetail(@PathVariable("applyId") Long applyId, Model model) {
        YwAppraisalApplyDO applyDO = applyService.selectOne(applyId);
        if (applyDO == null) {
            return "redirect:apply";
        }
        appraisalItem(model, applyDO);
        List<SysFileDO> fileDOS = null;
        YwAppraisalDataDO dataDO = null;
        if (applyDO.getDataId() != null) {
            dataDO = applyService.selectOneData(applyDO.getDataId());
            fileDOS = getSysFileDOS(dataDO, fileService);
        }
        model.addAttribute("dataDO", dataDO);
        model.addAttribute("fileDOS", fileDOS);
        model.addAttribute("apply", applyDO);

        return prefix + "/applyDetail";
    }


    @BsCLog("进入添加申请材料界面")
    @RequiresPermissions("yw:apply:add")
    @GetMapping(value = "/applyDataAdd")
    public String applyDataAdd() {
        return prefix + "/applyDataAdd";
    }

    @BsCLog("添加申请材料返回添加结果")
    @RequiresPermissions("yw:apply:add")
    @GetMapping(value = "/applyDataAdd/get")
    @ResponseBody
    public ReturnDTO applyDataAdd(String message, String fileId) {
        YwAppraisalDataDO dataDO = new YwAppraisalDataDO();
        dataDO.setMessage(message);
        dataDO.setFileId(fileId);
        dataDO.setCreateId(getUserId());
        Long id = applyService.insertData(dataDO);
        if (id != 0L) {
            return ReturnDTO.ok(id);
        } else {
            return ReturnDTO.error("申请材料添加失败");
        }
    }

    private void appraisalItem(Model model, YwAppraisalApplyDO applyDO) {
        if (applyDO.getItemThree() != null) {
            YwAppraisalItemDO itemDO = appraisalService.findItemById(applyDO.getItemThree());
            model.addAttribute("itemDO", itemDO);
        } else {
            YwAppraisalItemDO itemDO = appraisalService.findItemById(applyDO.getItemTwo());
            model.addAttribute("itemDO", itemDO);
        }
    }

    static List<SysFileDO> getSysFileDOS(YwAppraisalDataDO dataDO, SysFileService fileService) {
        List<SysFileDO> fileDOS = null;
        if (dataDO.getFileId() != null) {
            String[] strings = dataDO.getFileId().split(",");
            if (strings.length > 0) {
                Long[] ids = (Long[]) ConvertUtils.convert(strings, Long.class);
                fileDOS = fileService.selectList(ids);
            }
        }
        return fileDOS;
    }
}
